package com.chang.product.app;

import com.chang.common.utils.PageUtils;
import com.chang.common.utils.R;
import com.chang.product.dto.AttrGroupWithAttrsVo;
import com.chang.product.entity.AttrEntity;
import com.chang.product.entity.AttrGroupEntity;
import com.chang.product.service.AttrGroupService;
import com.chang.product.service.AttrService;
import com.chang.product.service.CategoryService;
import com.chang.product.vo.AttrGroupRelationVo;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author chang
 * @email 2045807586@qq.com
 * @date 2024-01-02 16:10:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    // 获取当前分类下的所有分组和属性 /product/attrgroup/{catelogId}/withattr

    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        // 查出当前分类下的所有属性分组,以及分组中的所有属性
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVos);
    }


    // 关联属性
    @PostMapping("attr/relation")
    public R attrRelation(@RequestBody @NotEmpty List<AttrGroupRelationVo> attrGroupRelationVos) {
        attrService.attrRelation(attrGroupRelationVos);
        return R.ok();
    }

    // 获取指定分组关联的所有属性
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrgroupIdAttrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    // 获取指定分组未关联的所有属性
    @GetMapping("{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    // 前端传递参数  [{"attrId":1,"attrGroupId":2}]
    // post请求携带的json数据想封装成为自定义对象必须加上@RequestBody
    @PostMapping("attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.attrRelationDelete(vos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCateLogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


}
